package com.whfc.ms.api.emp.controller;

import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppEmpInputDataDTO;
import com.whfc.emp.enums.AttendType;
import com.whfc.emp.param.AppEmpInputDataAddParam;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsAppEmpInputDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @author sun_guodong
 * @description 录入记录
 * @date 2020-08-03
 */
@RestController
@RequestMapping("/ms/api/emp/attend/inputData")
public class AppEmpInputDataController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsAppEmpInputDataService msAppEmpInputDataService;


    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startDate", required = false) Date startDate,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endDate", required = false) Date endDate,
                       @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("录入记录列表,loginer:{},deptId:{},pageNum:{},pageSize:{},startDate:{},endDate:{},keyword:{}",
                user.getUsername(), deptId, pageNum, pageSize, startDate, endDate, keyword);
        PageData<AppEmpInputDataDTO> pageData = msAppEmpInputDataService.list(deptId, pageNum, pageSize, startDate, endDate, keyword);
        return ResultUtil.success(pageData);
    }

    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody AppEmpInputDataAddParam param) {
        logger.info("后台手动添加考勤数据,loginer:{},param:{}", user.getUsername(), param.toString());
        param.setType(AttendType.MS.getValue());
        msAppEmpInputDataService.add(param);
        return ResultUtil.success();
    }

//    @PostMapping(value = {"/anon/qr", "/qr"})
//    public Result qr(@Validated @RequestBody AppEmpInputDataAddParam param) {
//        logger.info("扫码添加考勤数据,param:{}", param.toString());
//        param.setType(AttendType.QR.getValue());
//        msAppEmpInputDataService.add(param);
//        return ResultUtil.success();
//    }

    @PostMapping("/del/{id}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("id") Integer id) {
        logger.info("删除考勤记录,loginer:{},id:{}", user.getUsername(), id);
        msAppEmpInputDataService.del(id);
        return ResultUtil.success();
    }

    @PostMapping(value = {"/anon/upload", "/upload"})
    public Result upload(@RequestParam("file") MultipartFile file) {
        logger.info("上传打卡照片,file:{}", file.getOriginalFilename());
        OssPathDTO data = msAppEmpInputDataService.upload(file);
        return ResultUtil.success(data);
    }
}
